﻿  
  --get the addon namespace
  local addon, ns = ...
  
  --get the config values
  local LabelUI = ns.LabelUI
  local cfg = ns.cfg

  local noshow = LabelUI.noshow
  local Kill = LabelUI.Kill
  local textureBackground
  local texturehilite
  local textureBackground2
  
  local LoadConfig = function()
  	textureBackground = cfg.textureBackground
  	texturehilite = cfg.texturehilite
  	textureBackground2 = cfg.textureBackground2	
  end
  
  local masupdated = false
  local ftabs
  local function OnTabupdate(self)
	if not self.updated then return end
	self.bg:SetAlpha(self:GetAlpha())
	self.bg:SetPoint("LEFT", self, 0, 0)
	self.bg:SetPoint("RIGHT", self, -1.5, 0)
        local ctt =  _G[self:GetName().."Text"]
        if self:GetID() < 11 then
           ctt:SetTextColor(1,1,1,1)
        end
	local chatframe = _G["ChatFrame"..self:GetID()]
	local editbox = chatframe.editBox
	if not _G[editbox:GetName().."Header"]:IsShown() then _G[editbox:GetName().."HeaderSuffix"]:Hide() end
	if self:IsShown() then self.bg:Show() else self.bg:Hide() end
	local scrollframe = _G["GeneralDockManagerScrollFrame"]
	if chatframe.isDocked and not chatframe.isStaticDocked and self:IsShown() then
		if (self:GetRight() <= scrollframe:GetLeft()) or (self:GetLeft() >= scrollframe:GetRight()) then self.bg:Hide() end
		if (self:GetLeft() < scrollframe:GetLeft()) then
			self.bg:SetPoint("LEFT", scrollframe, 0, 0)		 
		end
		if (self:GetRight() > scrollframe:GetRight()) then
	 		self.bg:SetPoint("RIGHT", scrollframe, 0, 0)			 
		end
	end
  end
  local function OnTabenter(self)
	self.bg.h:Show()
  end
  local function OnTableave(self)
	self.bg.h:Hide()
  end
  local function UpdateChatTab(chatTab, editbox, chatframe)
	local function func(self)
	    local backg = (self:GetParent()).bg
      	    backg:SetBackdropBorderColor(0, 0, 0, 1)
      	    backg.h:SetBackdropBorderColor(0, 0, 0, 1)
         end 
         local function func2(self)
	    local backg = (self:GetParent()).bg
      	    backg:SetBackdropBorderColor(0, 0, 0, 0)
      	    backg.h:SetBackdropBorderColor(0, 0, 0, 0)
         end
         local ctl =  _G[chatTab:GetName().."Left"]
         local ctm =  _G[chatTab:GetName().."Middle"]
         local ctr =  _G[chatTab:GetName().."Right"]
         local ctsl =  _G[chatTab:GetName().."SelectedLeft"]
         local ctsm =  _G[chatTab:GetName().."SelectedMiddle"]
         local ctsr =  _G[chatTab:GetName().."SelectedRight"]
         local cthl =  _G[chatTab:GetName().."HighlightLeft"]
         local cthm =  _G[chatTab:GetName().."HighlightMiddle"]
         local cthr =  _G[chatTab:GetName().."HighlightRight"]
         local ctt =  _G[chatTab:GetName().."Text"]
	 local etm = _G[editbox:GetName().."Mid"]
	 local etl = _G[editbox:GetName().."Left"]
	 local etr = _G[editbox:GetName().."Right"]
	 local eth = _G[editbox:GetName().."Header"]
	 local eths = _G[editbox:GetName().."HeaderSuffix"]
	 local etfm = _G[editbox:GetName().."FocusMid"]
	 local etfl = _G[editbox:GetName().."FocusLeft"]
	 local etfr = _G[editbox:GetName().."FocusRight"]
         ctt:SetTextColor(1,1,1)
         ctt:SetFont(cfg.font, 12)
         eth:SetFont(cfg.font, 14)
         eths:SetFont(cfg.font, 14)
         editbox:SetFont(cfg.font, 14)
         Kill(ctl)
         Kill(ctm)
         Kill(ctr)
         Kill(ctsl)
         Kill(ctsr)
         Kill(cthl)
         Kill(cthr)
	 Kill(etm)
	 Kill(etl)
	 Kill(etr)
	 Kill(etfm)
	 Kill(etfl)
	 Kill(etfr)
	 Kill(cthm)
	 chatTab.bg = CreateFrame("Frame", nil, ftabs)
	 chatTab.bg:SetPoint("BOTTOM", chatTab, 0, 0.75) 
	 chatTab.bg:SetPoint("LEFT", chatTab, 0, 0)
	 chatTab.bg:SetPoint("RIGHT", chatTab, -1.5, 0)
	 chatTab.bg:SetHeight(22)
	 chatTab.bg:SetFrameStrata("BACKGROUND")
	 chatTab.bg:SetFrameLevel(chatTab:GetFrameLevel()-1)	 
	 chatTab.bg:SetBackdrop(textureBackground)
	 chatTab.bg:SetBackdropColor(0, 0, 0, 0.7)
	 if not chatTab:IsShown() then chatTab.bg:Hide() end
         if i == 1 then
         	chatTab.bg:SetBackdropBorderColor(0, 0, 0, 1)
	 else
         	chatTab.bg:SetBackdropBorderColor(0, 0, 0, 0)
	 end
	 chatTab.bg.h = CreateFrame("Frame", nil, chatTab.bg)
	 chatTab.bg.h:SetAllPoints(chatTab.bg)
	 chatTab.bg.h:SetBackdrop(texturehilite)
	 chatTab.bg.h:SetBackdropColor(0, 240/255, 1, 0.5)
         if i == 1 then
         	chatTab.bg.h:SetBackdropBorderColor(0, 0, 0, 1)
	 else
         	chatTab.bg.h:SetBackdropBorderColor(0, 0, 0, 0)
	 end
	 chatTab.bg.h:SetFrameLevel(chatTab.bg.h:GetFrameLevel()+1)	
	 chatTab.bg.h:Hide()
      	 if ctsm:IsShown() then func(ctsm) end
      	 Kill(ctsm, func, func2)
	 chatTab:SetScript("OnUpdate", OnTabupdate)
         chatTab:SetScript("OnEnter", OnTabenter)
         chatTab:SetScript("OnLeave", OnTableave)
	 chatTab:SetScript("OnHide", function (self) self.bg:Hide() end)
	 chatTab:SetScript("OnShow", function (self) self.bg:Show() end)
	 --chatTab:SetScript("OnDragStop", function (self) self:SetScript("OnUpdate", OnTabupdate) end)
	 editbox.bg = CreateFrame("Frame", nil, editbox)
	 editbox.bg:SetPoint("BOTTOM", editbox, 0, 6) 
	 editbox.bg:SetPoint("TOP", editbox, 0, -6) 
	 editbox.bg:SetWidth(editbox:GetWidth() - 12) 
	 editbox.bg:SetFrameLevel(editbox:GetFrameLevel()-1)	 
	 editbox.bg:SetBackdrop(textureBackground2)
	 editbox.bg:SetBackdropColor(0, 0, 0, 0.7)
         editbox.bg:SetBackdropBorderColor(0, 0, 0, 0)
	 local function Killborder(v)	 
	 	Kill(_G[v.."TopLeftTexture"])
	 	Kill(_G[v.."BottomLeftTexture"])
	 	Kill(_G[v.."TopRightTexture"])
	 	Kill(_G[v.."BottomRightTexture"])
		Kill(_G[v.."LeftTexture"])
	 	Kill(_G[v.."RightTexture"])
		Kill(_G[v.."BottomTexture"])
		Kill(_G[v.."TopTexture"])
	end
	Killborder(chatframe)
	local btnframe = chatframe.."ButtonFrame"
	Killborder(btnframe)
	_G[btnframe.."BottomButton"]:SetHighlightTexture("Interface\\AddOns\\LabelUI\\media\\btnhighlight")
	_G[btnframe.."BottomButton"]:SetNormalTexture("Interface\\AddOns\\LabelUI\\media\\btn\\scrolldownend")
	_G[btnframe.."BottomButton"]:SetPushedTexture("Interface\\AddOns\\LabelUI\\media\\btn\\scrolldownend_d")
    _G[btnframe.."BottomButton"]:SetDisabledTexture("Interface\\AddOns\\LabelUI\\media\\btn\\scrolldownend") 
	_G[btnframe.."BottomButtonFlash"]:SetTexture("Interface\\AddOns\\LabelUI\\media\\btn\\blink")
	_G[btnframe.."UpButton"]:SetHighlightTexture("Interface\\AddOns\\LabelUI\\media\\btnhighlight")
	_G[btnframe.."UpButton"]:SetNormalTexture("Interface\\AddOns\\LabelUI\\media\\btn\\scrollup")
	_G[btnframe.."UpButton"]:SetPushedTexture("Interface\\AddOns\\LabelUI\\media\\btn\\scrollup_d")
    _G[btnframe.."UpButton"]:SetDisabledTexture("Interface\\AddOns\\LabelUI\\media\\btn\\scrollup") 
	_G[btnframe.."MinimizeButton"]:SetHighlightTexture("Interface\\AddOns\\LabelUI\\media\\btnhighlight")
	_G[btnframe.."MinimizeButton"]:SetNormalTexture("Interface\\AddOns\\LabelUI\\media\\btn\\chatmini")
	_G[btnframe.."MinimizeButton"]:SetPushedTexture("Interface\\AddOns\\LabelUI\\media\\btn\\chatmini_d")
    _G[btnframe.."MinimizeButton"]:SetDisabledTexture("Interface\\AddOns\\LabelUI\\media\\btn\\chatmini") 
	_G[btnframe.."DownButton"]:SetHighlightTexture("Interface\\AddOns\\LabelUI\\media\\btnhighlight")
	_G[btnframe.."DownButton"]:SetNormalTexture("Interface\\AddOns\\LabelUI\\media\\btn\\scrolldown")
	_G[btnframe.."DownButton"]:SetPushedTexture("Interface\\AddOns\\LabelUI\\media\\btn\\scrolldown_d")
    _G[btnframe.."DownButton"]:SetDisabledTexture("Interface\\AddOns\\LabelUI\\media\\btn\\scrolldown") 
	 	 
         chatTab.updated = true
  end
  local function ChatFrameStyler_INT(self)
    if not masupdated then
    ftabs = CreateFrame("Frame", "ChatTabsFrames", UIParent)
    for i,v in pairs(CHAT_FRAMES) do 
       local chatTab = _G[v.."Tab"]
       local editbox = _G[v.."EditBox"]
	   local chatframe = v
	   --chatTab:SetScript("OnUpdate", OnTabupdate)	
       if not chatTab.updated then
       	   UpdateChatTab(chatTab, editbox,  chatframe)  
       end  
    end
	ChatFrameMenuButton:SetHighlightTexture("Interface\\AddOns\\LabelUI\\media\\btnhighlight")
	ChatFrameMenuButton:SetNormalTexture("Interface\\AddOns\\LabelUI\\media\\btn\\chat")
	ChatFrameMenuButton:SetPushedTexture("Interface\\AddOns\\LabelUI\\media\\btn\\chat_d")
    ChatFrameMenuButton:SetDisabledTexture("Interface\\AddOns\\LabelUI\\media\\btn\\chat")
	FriendsMicroButton:SetHighlightTexture("Interface\\AddOns\\LabelUI\\media\\btn\\bbro_h")
	FriendsMicroButton:SetNormalTexture("Interface\\AddOns\\LabelUI\\media\\btn\\bbro")
	FriendsMicroButton:SetPushedTexture("Interface\\AddOns\\LabelUI\\media\\btn\\bbro")
    FriendsMicroButton:SetDisabledTexture("Interface\\AddOns\\LabelUI\\media\\btn\\bbro") 
	FriendsMicroButton:SetPoint("BOTTOM", DEFAULT_CHAT_FRAME.buttonFrame, "TOP", 0, -2)
    if Aurora then
    local t = _G["GeneralDockManagerOverflowButtonList"]
    t:SetBackdrop(nil)
    local bg = CreateFrame("Frame", nil, t)
    bg:SetPoint("TOPLEFT")
    bg:SetPoint("BOTTOMRIGHT")
    bg:SetFrameLevel(t:GetFrameLevel()-1)
    Aurora[1].CreateBD(bg, .5, 1)
    end
    masupdated = true
    end
    --DEFAULT_CHAT_FRAME:AddMessage(self:GetName())
    local chatTab = _G[self:GetName().."Tab"]
    local editbox = _G[self:GetName().."EditBox"]
	local chatframe = self:GetName()
    if not chatTab:GetScript("OnUpdate") and chatTab.updated then
	chatTab:SetScript("OnUpdate", OnTabupdate)		
    end
    if not chatTab.updated then   
      UpdateChatTab(chatTab, editbox, chatframe)
    end
  end
  
  --[[local function ChatFrameStyler_Update(self)
    --DEFAULT_CHAT_FRAME:AddMessage(self:GetName())
    local chatTab = _G[self:GetName().."Tab"]
    local editbox = _G[self:GetName().."EditBox"]
	local chatframe = self:GetName()
    UpdateChatTab(chatTab, editbox, chatframe)
	if not chatTab:GetScript("OnUpdate") and chatTab.updated then
	chatTab:SetScript("OnUpdate", OnTabupdate)		
    end
    if not chatTab.updated then   
      UpdateChatTab(chatTab, editbox, chatframe)
    end
  end]]--

  local function ChatEdit_UpdateBorderColor(editBox)
	local type = editBox:GetAttribute("chatType")
    	if ( not type ) then
        	return
    	end
 
    	local info = ChatTypeInfo[type]
	
	if editBox.bg then
      		editBox.bg:SetPoint("BOTTOM", editBox, 0, 6) 
      		editBox.bg:SetPoint("TOP", editBox, 0, -6) 
      		editBox.bg:SetWidth(editBox:GetWidth() - 12) 
		if ACTIVE_CHAT_EDIT_BOX then
			editBox.bg:SetBackdropBorderColor(info.r, info.g, info.b, 1)
		else
			editBox.bg:SetBackdropBorderColor(0, 0, 0, 0)
		end
	end
  end

  local function ChatEdit_Activate(editBox)
	local type = editBox:GetAttribute("chatType")
    	if ( not type ) then
        	return
    	end
 
    	local info = ChatTypeInfo[type]
	--_G[editBox:GetName().."HeaderSuffix"]:trueShow()
	if editBox.bg then
		editBox.bg:SetBackdropBorderColor(info.r, info.g, info.b, 1)
	end
  end

  local function ChatEdit_DeActivate(editBox)	
   	_G[editBox:GetName().."HeaderSuffix"]:Hide()
	if editBox.bg then
		editBox.bg:SetBackdropBorderColor(0, 0, 0, 0)
	end
  end
  
  ---------------------------------------
  -- CALLS // HOOKS
  ---------------------------------------
  local function Init()
  
  	LoadConfig()
  
  	hooksecurefunc("FloatingChatFrame_OnLoad",		ChatFrameStyler_INT)
  	--hooksecurefunc("ChatFrame_OnUpdate",		ChatFrameStyler_INT)
	--hooksecurefunc("ChatFrame_OnLoad",		ChatFrameStyler_INT)
  	--for i = 1,NUM_CHAT_WINDOWS do
		ChatFrameStyler_INT(_G["ChatFrame1"])
	--end
	hooksecurefunc("FCFTab_OnUpdate",      	OnTabupdate)
  	hooksecurefunc("ChatEdit_UpdateHeader",		ChatEdit_UpdateBorderColor)
  	hooksecurefunc("ChatEdit_DeactivateChat",		ChatEdit_DeActivate)
  	hooksecurefunc("ChatEdit_ActivateChat",		ChatEdit_Activate)
  end
  LabelUI:AddInit(Init, "StyleChat")